/** Created by lookiw on 2016/3/18.
 * 1.发送手机验证码
 * 2.注册*/
//发送验证码
var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount; //当前剩余秒数
function SetRemainTime() {
	if(curCount == 0) {
		window.clearInterval(InterValObj);
		//停止计时器
		$("#send-validation").removeAttr("disabled");
		//启用按钮
		$("#send-validation").html("重新获取");
	} else {
		curCount--;
		$("#send-validation").html(curCount + "秒");
	}
}

function sendSms() {
	var _phone = $("#phone").val();
	console.log($("#validation9").val());
	console.log(GetUrl() + "/data/api/asms_verify_for_web?visitType=web&phone=" + _phone + "&user_code=" + $("#validation9").val() + "&id=" + getCookie("img_id"));
	$.ajax({
		type: "get",
		url: GetUrl() + "/data/api/asms_verify_for_web?visitType=web&phone=" + _phone + "&user_code=" + $("#validation9").val() + "&id=" + getCookie("img_id"),
		timeout: 30000,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			delCookie("img_id")
			msgError("发送验证失败！" + textStatus);
		},
		success: function(data) {
			console.log(data);
			delCookie("img_id")
			var obj = eval(data);
			if(obj.success == false) {
				msgError(obj.data);
				$("#regist").attr("disabled", "true");
			} else {
				console.log("发送成功");
			}
		}
	});
}
//判断手机号码有没有被注册
function isPhoneRegist() {
	var phone = $("#phone").val();
	if(phone == null || phone.length != 11) {
		msgError("手机号码格式不正确");
		return false
	}
	$.ajax({
		type: "get",
		url: GetUrl() + "/data/api/is_registered?visitType=web&phone=" + phone,
		timeout: 30000,
		success: function(data) {
			console.log(data);
			var obj = eval(data);
			console.log(obj);
			if(obj.data == true) {
				msgError("该用户已注册过");
				$("#regist").attr("disabled", "true");

			} else {
				$("#regist").removeAttr("disabled");
				curCount = count;
				//设置button效果，开始计时
				$("#send-validation").attr("disabled", "true");
				$("#send-validation").html(curCount + "秒");
				InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
				sendSms();

			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(textStatus);
			console.log(errorThrown);
			msgError("访问服务器失败，请稍后再试");
			console.log("访问服务器失败，请稍后再试");
		}
	});

}

function register() {
	//phone 电话； verify_code 短信验证码 ；password 密码 ； inviter_phone 邀请人手机号
	var inviter_phone = GetRequest().phone;
	var phone = $('#phone').val();
	var pwd = $("#password").val();
	var repwd = $("#repassword").val();
	var validation = $("#validation").val();
	if(!phone) {
		msgError("手机号不能为空");
		return;
	}
	if(!(/^1[3|4|5|6|7|8]\d{9}$/.test(phone))) {
		msgError("手机号码不正确");
		return;
	}
	if(!inviter_phone) {
		msgError("邀请人手机号不能为空");
		return;
	}
	if(!(/^1[3|4|5|6|7|8]\d{9}$/.test(inviter_phone))) {
		msgError("邀请人手机号不正确");
		return;
	}
	if(pwd == null || pwd.length == 0) {
		msgError("密码不能为空");
		return;
	}
	if(pwd != repwd) {
		msgError("两次密码不一致");
		return;
	}
	if(validation == null || validation.length == 0) {
		msgError("验证码不能为空");
		return;
	}
	var yzm = $("#validation").val();
	var param = new Object();
	param.phone = phone;
	param.inviter_phone = inviter_phone;
	param.password = pwd;
	param.verify_code = yzm;
	console.log(param);
	var temp = encryptForBack(param);
	$.ajax({
		type: "post",
		dataType: "json",
		data:temp,
		url: GetUrl() + "/data/api/register_json_for_web?visitType=web",
		headers: {
			'Content-Type': 'text/plain;charset=utf-8'
		},
		success: function(data) {
			console.log(data);
			if(data.code == "000") {
				window.location.href = "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzI2MjY1MzY0OA==&scene=124#wechat_redirect";
			} else {
				msgError(data.msg);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log("XMLHttpRequest.status = " + XMLHttpRequest.status);
			console.log("XMLHttpRequest.readyState = " + XMLHttpRequest.readyState);
			console.log("textStatus = " + textStatus);
		}
	});
}